import React, {Component} from 'react';
import reactDom from 'react-dom';

import Uploader from '../../../../../public/components/UploaderImgWithBase64';

import {
    toast
} from 'nc-lightapp-front';

import language from '../../../language';
import {getColor} from '../../../../../../hrpub/common/utils/utils';
import init from "./init";

const imageList = ['.png', '.jpg', '.gif', '.jpeg'];

export default function () {

    if (document.getElementById('photoDiv')) {
        return;
    }

    const {
        form: {
            setFormItemsVisible
        },
        imageData,
        MultiInit,
        photoConf
    } = this.props;


    setFormItemsVisible('bd_psndoc', {
        photo: false
    });
    let timer = null;
    const check = () => {
        let oFormBox = document.getElementById('form_bd_psndoc').children[0];
        // let box = oFormBox.children[0].children[0].children[0];
        // form写法变了， 比之前多了3层 先通过下面的方法获取
        let obox = oFormBox.querySelectorAll("div[class='group-form-item lightapp-component-form show-form']")[0];
        if (obox) {
            let oDiv = document.createElement('div');
            oDiv.id = 'photoDiv';
            oDiv.style.float = 'right';
            oDiv.style.marginRight = '10px';
            oDiv.style.marginTop = '8px';
            oDiv.style.backgroundColor = getColor().bgColor;


            obox.insertBefore(oDiv, obox.children[0]);

            reactDom.render(<Uploader
                label={photoConf.label}
                ref={(ref) => {
                    this.props.setUpLoaderRef(ref);
                }}
                onUpload={(result, file) => {
                    let filename = file.name;
                    let fileExt = filename.substring(filename.lastIndexOf('.'));

                    if (imageList.includes(fileExt.toLowerCase())) {
                        this.props.updateState({
                            imageData: result
                        })
                    } else {
                        toast({
                            color: 'danger',
                            content: language['hi6007-000177'] /* 上传格式不正确 */
                        });

                        return false;
                    }
                }}
                value={imageData}
                MultiInit={MultiInit}
            />, oDiv);
            if (timer) {
                clearTimeout(timer)
            }
        } else {
            timer = setTimeout(check, 100)
        }
    }
    check();
}
